package pl.koszolko.documentclustering.cluster.document;

import java.util.List;

import pl.koszolko.documentclustering.dao.Author;
import pl.koszolko.documentclustering.dao.Document;

public class DocumentClusteringByAuthor extends DocumentForClustering {
	private static final long serialVersionUID = 1L;
	
	public DocumentClusteringByAuthor(Document document) {
		super(document);
	}
	
	@Override
	public double countSimilarity(DocumentForClustering document) {
		if(document instanceof DocumentClusteringByAuthor) {
			return countSimilarityByAuthor(document);
		}
		throw new RuntimeException("Obiekt document posiada niewlasciwy typ");
	}

	private double countSimilarityByAuthor(DocumentForClustering document) {
		List<Author> authors = document.getAuthors();
		double commonAuthors = 0;
		double allAuthors = authors.size();
		
		for(Author a : getAuthors()) {
			if(authors.contains(a)) {
				++commonAuthors;
			} else {
				++allAuthors;
			}
		}
		
		if(allAuthors == 0) {
			return 0D;
		}
		
		return commonAuthors/allAuthors;
	}

}
